//-
//- InputHandler.java
//-
package com.ntf.games.framework.impl.gl;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.ArrayList;

import com.ntf.games.framework.Vector2f;

//-
//- @author Me
//-
//- 
//-
public class InputHandler implements KeyListener, FocusListener, MouseListener, MouseMotionListener, MouseWheelListener
{

  // //////////////////////
  // - Member Data
  public boolean[]           m_keys;
  public Vector2f            m_mouse;
  public boolean             m_mouseButton1Pressed;
  public boolean             m_mouseButton2Pressed;
  public boolean             m_mouseButton3Pressed;
  public Vector2f            m_lastMouse;
  public int                 m_mouseWheel;
  // //////////////////////
  public ArrayList<KeyEvent> m_keyEvents;

  public InputHandler()
  {
    this.m_keys = new boolean[65536];
    this.m_mouse = new Vector2f();
    this.m_lastMouse = new Vector2f();
    this.m_mouseWheel = 0;
    // -
    this.m_keyEvents = new ArrayList<KeyEvent>( 10);

  }

  // //////////////////////
  // - Member Functions
  // - (non-Javadoc)
  // - @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
  // -
  @Override
  public void mouseDragged( MouseEvent mouseEvent)
  {
    // TODO Auto-generated method stub
    this.m_mouse.x = mouseEvent.getX();
    this.m_mouse.y = mouseEvent.getY();
  }

  public float getDX()
  {
    return(this.m_lastMouse.x - this.m_mouse.x);
  }

  public float getDY()
  {
    return(this.m_lastMouse.y - this.m_mouse.y);
  }

  // - (non-Javadoc)
  // - @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
  // -
  @Override
  public void mouseMoved( MouseEvent mouseEvent)
  {
    // TODO Auto-generated method stub
    // int x = this._mouse.x;
    // int y = this._mouse.y;
    if( this.m_lastMouse != this.m_mouse)
    {
      this.m_lastMouse.x = this.m_mouse.x;
      this.m_lastMouse.y = this.m_mouse.y;
      this.m_mouse.x = mouseEvent.getX();
      this.m_mouse.y = mouseEvent.getY();
    }

    // System.out.printf( "[*] Mouse: Last: x:[%d] y:[%d] To x:[%d] y:[%d]]\n", this._lastMouse.x, this._lastMouse.y, this._mouse.x, this._mouse.y);
  }

  // - (non-Javadoc)
  // - @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
  // -
  @Override
  public void mouseClicked( MouseEvent mouseEvent)
  {
    // TODO Auto-gener0ated method stub
    System.out.printf( "[*] InputHandler.mouseClicked: x:[%d] y:[%d]\n", mouseEvent.getX(), mouseEvent.getY());

  }

  // - (non-Javadoc)
  // - @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
  // -
  @Override
  public void mouseEntered( MouseEvent mouseEvent)
  {
    // TODO Auto-generated method stub
    // System.out.printf( "[*] InputHandler.mouseEntered\n");

  }

  // - (non-Javadoc)
  // - @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
  // -
  @Override
  public void mouseExited( MouseEvent mouseEvent)
  {
    // TODO Auto-generated method stub
    // System.out.printf( "[*] InputHandler.mouseExited\n");

  }

  // - (non-Javadoc)
  // - @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
  // -
  @Override
  public void mousePressed( MouseEvent mouseEvent)
  {
    if( mouseEvent.getButton() == MouseEvent.BUTTON1)
    {
      this.m_mouseButton1Pressed = true;
      this.m_mouse.x = mouseEvent.getX();
      this.m_mouse.y = mouseEvent.getY();
    }
    else if( mouseEvent.getButton() == MouseEvent.BUTTON2)
    {
      this.m_mouseButton2Pressed = true;
    }
    else if( mouseEvent.getButton() == MouseEvent.BUTTON3)
    {
      this.m_mouseButton3Pressed = true;
    }
  }

  // - (non-Javadoc)
  // - @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
  // -
  @Override
  public void mouseReleased( MouseEvent mouseEvent)
  {
    if( mouseEvent.getButton() == MouseEvent.BUTTON1)
    {
      this.m_mouseButton1Pressed = false;
    }
    else if( mouseEvent.getButton() == MouseEvent.BUTTON2)
    {
      this.m_mouseButton2Pressed = false;
    }
    else if( mouseEvent.getButton() == MouseEvent.BUTTON3)
    {
      this.m_mouseButton3Pressed = false;
    }

  }

  // - @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
  // -
  @Override
  public void mouseWheelMoved( MouseWheelEvent mouseEvent)
  {
    // TODO Auto-generated method stub
    // int wheel = mouseEvent.getWheelRotation();
    // System.out.printf( "[*] MouseWheelEvent: [%d][%f][%f]\n", wheel, wheel / 0.0001, wheel * 0.0001);
    this.m_mouseWheel += mouseEvent.getWheelRotation();

  }

  // - (non-Javadoc)
  // - @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
  // -
  @Override
  public void focusGained( FocusEvent e)
  {
    // TODO Auto-generated method stub

  }

  // - (non-Javadoc)
  // - @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
  // -
  @Override
  public void focusLost( FocusEvent e)
  {
    for( int i = 0; i < this.m_keys.length; i++)
    {
      this.m_keys[i] = false;
    }
  }

  // - (non-Javadoc)
  // - @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
  // -
  @Override
  public void keyPressed( KeyEvent keyevent)
  {
    // TODO Auto-generated method stub
    int code = keyevent.getKeyCode();
    if( (code > 0) && code < (this.m_keys.length))
    {
      this.m_keys[code] = true;
      // System.out.printf( "[*] KeyEvent: code:[%d]:[%s]\n", code, KeyEvent.getKeyText( code));

    }
  }

  // - (non-Javadoc)
  // - @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
  // -
  @Override
  public void keyReleased( KeyEvent keyevent)
  {
    // TODO Auto-generated method stub
    int code = keyevent.getKeyCode();
    if( (code > 0) && code < (this.m_keys.length))
    {
      if( code != KeyEvent.VK_ESCAPE)
      {
        this.m_keys[code] = false;
      }
    }
  }

  // - (non-Javadoc)
  // - @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
  // -
  @Override
  public void keyTyped( KeyEvent keyEvent)
  {
    // TODO Auto-generated method stub

  }

}
